<?php

require_once dirname(__FILE__) . '/../../../../Domain51UnitTest.config.php';
require_once 'Domain51/Service/Amazon/Util/AuthorizationGenerator.php';
require_once 'Crypt/HMAC.php';

class Domain51_Service_Amazon_Util_AuthorizationGeneratorTest 
    extends PHPUnit_Framework_TestCase
{
    public function testGeneratesHeaderHash() {
        $config = array(
            'access_key' => 12345,
            'secret_key' => 54321
        );
        
        $testDate = gmdate(DATE_RFC822);
        $amzDate = gmdate(DATE_RFC822);
        
        $requestTypes = array('GET', 'POST', 'PUT', 'DELETE');
        $requestType = $requestTypes[rand(0, 3)];
        
        $contentTypes = array('text/html', 'text/xml');
        $contentType = $contentTypes[rand(0, 1)];
        
        $aclTypes = array('private', 'public-read', 'public-read-write');
        $aclType = $aclTypes[rand(0, 2)];
        
        $url = '/bucket/' . rand(1000, 2000);
        
        $header = new Domain51_Service_Amazon_Util_Header();
        $header->setType($requestType);
        $header->setUrl($url);
        $header->add('Content-Type', $contentType);
        $header->add('Date', $testDate);
        $header->addAmz('Acl', $aclType);
        $header->addAmz('Date', $amzDate);
        
        $auth = new Domain51_Service_Amazon_Util_AuthorizationGenerator($config);
        $auth->setHeader($header);
        $signature = $auth->generateHash();
        
        // setup expected signature
        $stringToSign = 
            "{$requestType}\n" .
            "\n" .
            "{$contentType}\n" .
            "{$testDate}\n" . 
            "x-amz-acl:{$aclType}\n" .
            "x-amz-date:{$amzDate}\n" .
            "$url";
        $crypt = new Crypt_HMAC($config['secret_key'], 'sha1');
        $hex = new Domain51_Util_String_Hex();
        $expectedSignature = $hex->toBase64($crypt->hash($stringToSign));
        
        $this->assertSame($expectedSignature, $signature);
    }
    
    public function testGeneratesFullAuthorizationHeader() {
        $config = array(
            'access_key' => 12345,
            'secret_key' => 54321
        );
        
        $testDate = gmdate(DATE_RFC822);
        $amzDate = gmdate(DATE_RFC822);
        
        $requestTypes = array('GET', 'POST', 'PUT', 'DELETE');
        $requestType = $requestTypes[rand(0, 3)];
        
        $contentTypes = array('text/html', 'text/xml');
        $contentType = $contentTypes[rand(0, 1)];
        
        $aclTypes = array('private', 'public-read', 'public-read-write');
        $aclType = $aclTypes[rand(0, 2)];
        
        $url = '/bucket/' . rand(1000, 2000);
        
        $header = new Domain51_Service_Amazon_Util_Header();
        $header->setType($requestType);
        $header->setUrl($url);
        $header->add('Content-Type', $contentType);
        $header->add('Date', $testDate);
        $header->addAmz('Acl', $aclType);
        $header->addAmz('Date', $amzDate);
        
        $auth = new Domain51_Service_Amazon_Util_AuthorizationGenerator($config);
        $auth->setHeader($header);
        $signature = $auth->generate();
        
        // setup expected signature
        $stringToSign = 
            "{$requestType}\n" .
            "\n" .
            "{$contentType}\n" .
            "{$testDate}\n" . 
            "x-amz-acl:{$aclType}\n" .
            "x-amz-date:{$amzDate}\n" .
            "$url";
        $crypt = new Crypt_HMAC($config['secret_key'], 'sha1');
        $hex = new Domain51_Util_String_Hex();
        $expectedSignature = "AWS {$config['access_key']}:" . $hex->toBase64($crypt->hash($stringToSign));
        
        $this->assertSame($expectedSignature, $signature);
    }
    
    public function testGeneratesHashThatMatchesAwsExampleOne() {
        $authGen = Solar::factory(
            'Domain51_Service_Amazon_Util_AuthorizationGenerator',
            array(
                'access_key' => "44CF9590006BF252F707",
                'secret_key' => "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV"
            )
        );
        
        $header = new Domain51_Service_Amazon_Util_Header();
        
        $header->setType('PUT');
        $header->setUrl('/quotes/nelson');
        $header->add('Content-Md5', 'c8fdb181845a4ca6b8fec737b3581d76');
        $header->add('Content-Type', 'text/html');
        $header->add('Date', 'Thu, 17 Nov 2005 18:49:58 GMT');
        $header->add('X-Amz-Magic', 'abracadabra'); 
        $header->add('X-Amz-Meta-Author', 'foo@bar.com');

        
        $authGen->setHeader($header);
        $this->assertSame(
            'jZNOcbfWmD/A/f3hSvVzXZjM2HU=',
            $authGen->generateHash()
        );
    }
    
    public function testStripsOutAllQueryParametersOfUri() {
        $authGen = Solar::factory(
            'Domain51_Service_Amazon_Util_AuthorizationGenerator',
            array(
                'access_key' => "44CF9590006BF252F707",
                'secret_key' => "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV"
            )
        );
        
        $header = new Domain51_Service_Amazon_Util_Header();
        
        $header->setType('PUT');
        $header->setUrl('/quotes/nelson?key=value&key2=value2');
        $header->add('Content-Md5', 'c8fdb181845a4ca6b8fec737b3581d76');
        $header->add('Content-Type', 'text/html');
        $header->add('Date', 'Thu, 17 Nov 2005 18:49:58 GMT');
        $header->add('X-Amz-Magic', 'abracadabra'); 
        $header->add('X-Amz-Meta-Author', 'foo@bar.com');

        
        $authGen->setHeader($header);
        $this->assertSame(
            'jZNOcbfWmD/A/f3hSvVzXZjM2HU=',
            $authGen->generateHash()
        );
    }
}

?>